package com.pjedu.nbmallwebsitesystem.web;

import com.pjedu.nbmallcommon.common.Constants;
import com.pjedu.nbmallcommon.common.MallException;
import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.BeanUtil;
import com.pjedu.nbmallshop.entity.MallGoodsInfo;
import com.pjedu.nbmallshop.service.MallGoodsCategoryService;
import com.pjedu.nbmallshop.service.MallGoodsInfoService;
import com.pjedu.nbmallshop.vo.MallGoodsDetailVO;
import com.pjedu.nbmallshop.vo.SearchPageCategoryVO;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class GoodsController {

	@Resource
	private MallGoodsInfoService mallGoodsInfoService;

	@Resource
	private MallGoodsCategoryService mallGoodsCategoryService;

	@GetMapping({ "/search", "/search.html" })
	public String searchPage(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		if (StringUtils.isEmpty(params.get("page"))) {
			params.put("page", 1);
		}
		params.put("limit", Constants.GOODS_SEARCH_PAGE_LIMIT);
		// 封装分类数据
		if (params.containsKey("goodsCategoryId") && !StringUtils.isEmpty(params.get("goodsCategoryId") + "")) {
			Long categoryId = Long.valueOf(params.get("goodsCategoryId") + "");
			SearchPageCategoryVO searchPageCategoryVO = mallGoodsCategoryService.getCategoriesForSearch(categoryId);
			if (searchPageCategoryVO != null) {
				request.setAttribute("goodsCategoryId", categoryId);
				request.setAttribute("searchPageCategoryVO", searchPageCategoryVO);
			}
		}
		// 封装参数供前端回显
		if (params.containsKey("orderBy") && !StringUtils.isEmpty(params.get("orderBy") + "")) {
			request.setAttribute("orderBy", params.get("orderBy") + "");
		}
		String keyword = "";
		// 对keyword做过滤 去掉空格
		if (params.containsKey("keyword") && !StringUtils.isEmpty((params.get("keyword") + "").trim())) {
			keyword = params.get("keyword") + "";
		}
		request.setAttribute("keyword", keyword);
		params.put("keyword", keyword);
		// 搜索上架状态下的商品
		params.put("goodsSellStatus", Constants.SELL_STATUS_UP);
		// 封装商品数据
		HashMap pageUtil = new HashMap(params);
		request.setAttribute("pageResult", mallGoodsInfoService.searchMallGoods(pageUtil));
		return "mall/search";
	}

	// 详情页
	@GetMapping("/detail")
	public String detailPage(Long goodsId, HttpServletRequest request) {
		if (goodsId < 1) {
			return "error/error_5xx";
		}
		MallGoodsInfo goods = mallGoodsInfoService.getMallGoodsById(goodsId);
		if (goods == null) {
			MallException.fail(ServiceResultEnum.GOODS_NOT_EXIST.getResult());
		}
		if (Constants.SELL_STATUS_UP != goods.getGoodsSellStatus()) {
			MallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
		}
		MallGoodsDetailVO goodsDetailVO = new MallGoodsDetailVO();
		BeanUtil.copyProperties(goods, goodsDetailVO);
		goodsDetailVO.setGoodsCarouselList(goods.getGoodsCarousel().split(","));
		request.setAttribute("goodsDetail", goodsDetailVO);
		return "mall/detail";
	}

}
